﻿

function init(options) {

    NODES.txtUser.setText('欢迎您：<span style="font-weight:bold;color:red;">' + options.userName + '</span> [' + options.userIP + ']');
    NODES.txtOnlineUserCount.setText('在线用户：' + options.onlineUserCount);

    //    Ext.DomQuery.selectNode('iframe', button.ownerCt.ownerCt.getEl().dom).contentWindow.location.replace(href);
    NODES.btnRefresh.on('click', function() {
        var iframe = Ext.DomQuery.selectNode('iframe', NODES.mainTabStrip.getActiveTab().body.dom);
        iframe.src = iframe.src;
    });

    // 显示/隐藏标题
    NODES.btnShowHideHeader.on('click', function() {
        if (NODES.regionTop.getInnerHeight() > 0) {
            NODES.regionTop.setHeight(0);
            NODES.btnShowHideHeader.setTooltip('显示标题栏');
            NODES.btnShowHideHeader.setIcon(options.showHeaderIconUrl);
        } else {
            NODES.regionTop.setHeight(60);
            NODES.btnShowHideHeader.setTooltip('隐藏标题栏');
            NODES.btnShowHideHeader.setIcon(options.hideHeaderIconUrl);
        }
        NODES.regionPanel.doLayout();
    });

    // 当前时间并定时更新
    setCurrentTime();
    window.setInterval(setCurrentTime, 30 * 1000);

    // Click the tree node.
    NODES.treeMenu.on('click', function(node, event) {
        addTabFromNode(node);
        // Don't response to this tree node's default behavior. 
        event.stopEvent();
    });


    // 根据URL的HASH值，选中左侧的树节点，以及添加右侧的Tab
    var currentHash = window.location.hash.substr(1);
    var rootNode = NODES.treeMenu.getRootNode();

    //    function getParentNodes(node) {
    //        var result = [];
    //        if (node.parentNode != rootNode) {
    //            result.push(node.parentNode);
    //            result = result.concat(getParentNodes(node.parentNode));
    //        }
    //        return result;
    //    }

    function getHashNode(node) {
        var result;
        if (node.attributes.href === currentHash) {
            result = node;
        } else {
            var childs = node.childNodes, i, count;
            if (childs && childs.length > 0) {
                for (i = 0, count = childs.length; i < count; i++) {
                    child = childs[i];
                    result = getHashNode(child);
                    if (result) {
                        break;
                    }
                }
            }
        }
        return result;
    }

    if (currentHash) {
        var hashNode = getHashNode(rootNode);
        if (hashNode) {
            NODES.treeMenu.expandPath(hashNode.parentNode.getPath());
            (function() {
                // 重新获取这个节点，此时此节点已经渲染到页面中
                hashNode = NODES.treeMenu.getNodeById(hashNode.id);
                hashNode.select();
                addTabFromNode(hashNode);
            }).defer(100);
        }
    }

}

// 只有点击左侧树中的链接才会改变URL的HASH值
function addTabFromNode(node) {
    var url = node.attributes.href;
    if (url) {
        window.location.href = '#' + url;
        addTab(node.id, url, node.text);
    }
}

function addTab(id, url, title) {
    // Add a dynamic tab.
    var tabID = 'dynamic_added_tab' + id.replace('__', '-');
    NODES.mainTabStrip.addTab({
        'id': tabID,
        'url': url,
        'title': title,
        'closable': true
    });
}


// 设置当前时间
function setCurrentTime() {
    var today = new Date();
    //year = today.getFullYear().toString().substr(2);
    month = String.leftPad(today.getMonth() + 1, '2', '0');
    date = String.leftPad(today.getDate(), '2', '0');
    hour = String.leftPad(today.getHours(), '2', '0');
    minute = String.leftPad(today.getMinutes(), '2', '0');
    //second = String.leftPad(today.getSeconds(), '2', '0');

    NODES.txtCurrentTime.setText("当前时间：" + month + "-" + date + " " + hour + ":" + minute);
}
